package br.edu.ifrn.sgpapp.rest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import br.edu.ifrn.sgp.dtos.NoticiaDTO;

public class NoticiaREST {
	
	public static List<NoticiaDTO> listarUltimasNoticias() throws RestClientException {
		
		String url = Enderecos.URL_PRINCIPAL_SGP + "noticia/ultimas";
		
		RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        
        NoticiaDTO[] noticias = restTemplate.getForObject(url, NoticiaDTO[].class);
        
        List<NoticiaDTO> resultado = new ArrayList<NoticiaDTO>();
        
        if (noticias != null){
        	resultado = Arrays.asList(noticias);
        }
		
		return resultado;
	}
	
}
